package cn.schoolwow.quickdao.module.database.dql.query.service;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.dql.response.flow.common.GetResultSetArrayFlow;
import cn.schoolwow.quickdao.module.database.parent.domain.GetStatementSupplier;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.GetAndCacheStatementFlow;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.ArrayList;

public class FetchNullFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setStatement(flowContext);
        setParameters(flowContext);
        executeStatement(flowContext);
    }

    @Override
    public String name() {
        return "查询字段为NULL的记录";
    }

    private void setStatement(FlowContext flowContext) {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        Entity entity = (Entity) flowContext.checkData("entity");
        String column = (String) flowContext.checkData("column");

        String sqlCacheName = "fetchNull_" + entity.tableName + "_" + column + "_" + quickDAOConfig.databaseContext.databaseProvider.name();
        GetStatementSupplier getStatementSupplier = new GetStatementSupplier() {
            @Override
            public String getStatement() {
                StringBuilder sqlBuilder = new StringBuilder("select ");
                for(Property property:entity.properties){
                    sqlBuilder.append(quickDAOConfig.databaseContext.databaseProvider.escape(property.column)+",");
                }
                sqlBuilder.deleteCharAt(sqlBuilder.length()-1);
                sqlBuilder.append(" from " + quickDAOConfig.databaseContext.databaseProvider.escape(entity.tableName));
                sqlBuilder.append(" where " + quickDAOConfig.databaseContext.databaseProvider.escape(column) + " is null");
                return sqlBuilder.toString();
            }
        };
        flowContext.startFlow(new GetAndCacheStatementFlow())
                .putCurrentCompositeFlowData("sqlCacheName", sqlCacheName)
                .putInstanceData(getStatementSupplier, GetStatementSupplier.class)
                .execute();
    }

    private void setParameters(FlowContext flowContext) {
        flowContext.putTemporaryData("parameters", new ArrayList<>());
    }

    private void executeStatement(FlowContext flowContext) throws Exception {
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "查询字段为NULL的记录")
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        flowContext.startFlow(new GetResultSetArrayFlow())
                                .putTemporaryData("resultSet", resultSet)
                                .execute();
                    }
                })
                .execute();
    }
}
